package com.youdianstar.app.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.youdianstar.app.common.ex.setThrottleListener
import com.youdianstar.app.common.util.DateUtil
import com.youdianstar.app.common.util.MathUtil
import com.youdianstar.app.common.util.StrUtil
import com.youdianstar.app.R
import com.youdianstar.app.databinding.LayoutItemWithdrawalBinding
import com.youdianstar.app.entity.WithdrawalRec
import com.youdianstar.app.util.DataAppManager


/**
 * File: WithdrawalRcAdapter
 * Author: 82149 Create: 2023/8/2 18:26
 * Changes (from 2023/8/2)
 * --------------------------------------------------
 * description: 商户中心--提现记录
 * ---------------------------------------------------
 *
 */
class WithdrawalRcAdapter(val mContext: Context, val dataList: List<WithdrawalRec>): RecyclerView.Adapter<WithdrawalRcAdapter.ListHolder>() {

    class ListHolder(val binding : LayoutItemWithdrawalBinding) : RecyclerView.ViewHolder(binding.root)

    var symbolTx:String = ""
    init {
        symbolTx = DataAppManager.instance.symbolTx()
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ListHolder {
        return ListHolder(LayoutItemWithdrawalBinding.inflate(LayoutInflater.from(parent.context), parent, false))
    }

    override fun getItemCount(): Int {
       return dataList.size
    }

    override fun onBindViewHolder(holder: ListHolder, position: Int) {
        dataList[position].let { item ->
            var stateStr = ""
            //状态：0-申请中 1-已结算 2-驳回申请
            when (item.state) {
                0 -> {
                    holder.binding.ivStatus.setBackgroundResource(R.drawable.oval_order_process)
                    stateStr = mContext.getString(R.string.shop_pendingReview)
                    holder.binding.clRecordPart2.visibility = View.GONE
                }
                2 -> {
                    holder.binding.ivStatus.setBackgroundResource(R.drawable.oval_order_loss)
                    stateStr = mContext.getString(R.string.shop_rejected)
                    //已驳回 -展开布局
                    holder.binding.clRecordPart2.visibility = View.VISIBLE
                }
                else -> {
                    holder.binding.ivStatus.setBackgroundResource(R.drawable.oval_order_complete)
                    stateStr = mContext.getString(R.string.shop_completed)
                    holder.binding.clRecordPart2.visibility = View.GONE
                }
            }
            holder.binding.tvName.text = stateStr
            holder.binding.tvTime.text = DateUtil.getOrderTime(item.createTime)
            holder.binding.tvAmount.text = symbolTx+MathUtil.round(item.amount)

            holder.binding.ivEpDex.rotation = if (item.isExp) 180F else 0F
            holder.binding.tvRemark.visibility = if (item.isExp) View.VISIBLE else View.GONE
            holder.binding.tvRemark.text = if (StrUtil.isEmpty(item.remark)) "--" else item.remark
        }

        holder.binding.txRemark.setThrottleListener {
            dataList[holder.bindingAdapterPosition].let { info ->
                val isExpNow = info.isExp
                updateItemViewExp(holder.bindingAdapterPosition, !isExpNow)
            }
        }
        holder.binding.tvRemark.setThrottleListener {
            dataList[holder.bindingAdapterPosition].let { info ->
                updateItemViewExp(holder.bindingAdapterPosition, false)
            }
        }
    }

    /**更新布局展开状态*/
    private fun updateItemViewExp(position: Int, expAction:Boolean){
        dataList[position].isExp = expAction
        notifyItemChanged(position)
    }
}